﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class InitializeHelper
    {
        /// <summary>
        /// 初始化客户端的ID 和 编号 数据库类型
        /// </summary>
        public static InitConfig InitClientIdAndNumber()
        {
            var initConfig = InitConfigDao.Instance.GetInitConfig();
            if(initConfig == null)
            {
                throw new Exception("LoadInitCofig 时发生严重错误。");
            }
            else
            {
                return initConfig;
            }
        }

        /// <summary>
        /// 初始化一个默认的管理账号(默认为开发者)
        /// </summary>
        public static void InitSysUser()
        {
            var devUser = UserDao.Instance.GetDefaultUser();
            if (devUser == null)
            {
                devUser =UserDao.Instance.GenerateDefuaultUser();
               UserDao.Instance.Sqlite.Insert(devUser);
            }
        }
        /// <summary>
        /// 设置当前的煤矿 
        /// </summary>
        public static void SetCurrentCompany()
        {
            var cid = Convert.ToInt64(App.InitConfig.companyId);
            var company = CompanyDao.Instance.FindById(cid);
            if (company == null)
            {
                new CompanyInitWindow().ShowDialog();
            }
            else
            {
                App.CurrentCompanyVo = new CompanyVo(company);
            }          
        }
    

        internal static void SetCurrentScaleHouse()
        {
            var id = Convert.ToInt64(App.InitConfig.scaleHouseId);
            var ScaleHouse = ScaleHouseDao.Instance.FindById(id);
            if (ScaleHouse == null)
            {
                App.CurrentScaleHouseVo = new ScaleHouseVo(new ScaleHouse() { id = -1, name = "默认磅房" });
            }
            else
            {
                App.CurrentScaleHouseVo = new ScaleHouseVo(ScaleHouse);              
            }
        }
        
    }
}
